<template>
	<view>
		<!-- 搜索框区域 -->
		<view class="search-box">
			<uni-search-bar @input="input" :radius="100" cancel-button="none">
			</uni-search-bar>
		</view>

		<!-- 搜索建议列表区域 -->
		<view class="sugg-list" v-if="searchResultsList.length !== 0">
			<view class="sugg-item" v-for="(item,index) in searchResultsList" :key="index" @click="gotoDetail(item)">
				<view class="goods_name">{{item.goods_name}}</view>
				<uni-icons type="arrowright" :size="16"></uni-icons>
			</view>
		</view>

		<!-- 搜索历史区域 -->
		<view class="history-box" v-else>
			<!-- 标题区域 -->
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="17px" @click="clean"></uni-icons>
			</view>
			<!-- 列表区域 -->
			<view class="history-list">
				<uni-tag :text="item" v-for="(item,index) in historys" :key="index" @click="gotoGoodsList(item)"></uni-tag>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 定时器
				timer: null,
				// 关键词
				keyword: '',
				// 搜索建议列表
				searchResultsList: [],
				// 搜索历史列表
				historyList: ['a', 'b']
			};
		},
		onLoad() {
			this.historyList = JSON.parse(uni.getStorageSync('keyword') || '[]')
		},
		methods: {
			// 输入事件的回调
			input(e) {
				clearTimeout(this.timer)
				this.timer = setTimeout(() => {
					this.keyword = e
					this.searchResults()
				}, 500);
			},
			// 获取搜索建议列表
			async searchResults() {
				if (this.keyword.length === 0) {
					this.searchResultsList = []
					return
				}
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/goods/qsearch', {
					query: this.keyword
				})
				if (res.meta.status !== 200) return uni.$showMsg()
				this.searchResultsList = res.message
				this.saveSearchHistory()
			},
			// 跳转到商品详情页
			gotoDetail(item) {
				uni.navigateTo({
					url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
				})
			},
			// 跳转到商品列表
			gotoGoodsList(keyword){
				uni.navigateTo({
					url:'/subpkg/goods_list/goods_list?query=' + keyword
				})
			},
			saveSearchHistory() {
				const set = new Set(this.historyList)
				set.delete(this.keyword)
				set.add(this.keyword)
				this.historyList = Array.from(set)
				// 将搜索记录持久化存储到本地
				uni.setStorageSync('keyword', JSON.stringify(this.historyList))
			},
			clean() {
				// 清空搜索历史列表
				this.historyList = []
				// 清空本地存储的搜索关键词历史列表
				uni.setStorageSync('keyword', '[]')
			}
		},
		computed: {
			historys() {
				// 将搜索历史列表反转显示
				return [...this.historyList].reverse()
			}
		}
	}
</script>

<style lang="scss">
	.search-box {
		position: sticky;
		top: 0%;
		z-index: 999;
	}

	.sugg-list {
		padding: 0 5px;

		.sugg-item {
			display: flex;
			align-items: center;
			justify-content: space-between;
			font-size: 12px;
			padding: 13px 0;
			border-bottom: 1px solid #efefef;

			.goods_name {
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
			}
		}
	}

	.history-box {
		padding: 0 5px;

		.history-title {
			display: flex;
			justify-content: space-between;
			height: 40px;
			align-items: center;
			font-size: 13px;
			border-bottom: 1px solid #efefef;
		}

		.history-list {
			display: flex;
			flex-wrap: wrap;

			.uni-tag {
				margin-top: 5px;
				margin-right: 5px;
			}
		}
	}
</style>